package com.yangweiqiao.myapplication;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private LinearLayout ll_1;
    private ImageView iv;
    private Bitmap drawingCache;
    private RelativeLayout rl;
    private PathMeasure mPathMeasure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll_1 = (LinearLayout) findViewById(R.id.ll_1);
        rl = (RelativeLayout) findViewById(R.id.rl);
        iv = (ImageView) findViewById(R.id.iv);
        initView();
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start();
            }
        });
    }
    private float[] mCurrentPosition = new float[2];

    private void start() {




        //设置可以缓存
        System.out.println("11111");
        ll_1.setDrawingCacheEnabled(true);
        System.out.println("2222");

        //设置缓存质量 高清质量
        ll_1.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        //得到缓存的图像
        System.out.println("3333");

        drawingCache = ll_1.getDrawingCache();
        //创建ImageView
        final ImageView imageView = new ImageView(this);
        System.out.println("4444");

        imageView.setImageBitmap(drawingCache);

        System.out.println(drawingCache.toString());

        //设置宽高
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
        System.out.println("555");

        rl.addView(imageView, params);
        //计算起始坐标
        int startLoc[] = new int[2];
        imageView.getLocationInWindow(startLoc);
        System.out.println("6666");

        //从中心开始
        //x 轴
        float startX = startLoc[0]  ;
        //y轴
        float startY = startLoc[1] ;

        //计算终点位置
        int[] ints = new int[2];
        iv.getLocationInWindow(ints);
        float toX = ints[0]  ;
        float toY = ints[1]  ;
        System.out.println("7777");


        //开始绘制贝赛尔曲线
        Path path = new Path();
        //移动到开始的坐标
        path.moveTo(startX, 0);
        System.out.println("startX:"+startX);
        System.out.println("startY:"+startY);
        //二次贝塞尔曲线
        path.lineTo( toX, -toY );
        System.out.println("tox:"+toX);
        System.out.println("toy:"+toY);
        //mPathMeasure用来计算贝塞尔曲线的曲线长度和贝塞尔曲线中间插值的坐标，
        // 如果是true，path会形成一个闭环
        mPathMeasure = new PathMeasure(path, false);
        System.out.println("88888");

        //★★★属性动画实现（从0到贝塞尔曲线的长度之间进行插值计算，获取中间过程的距离值）

        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());

        valueAnimator.setDuration(1000);        System.out.println("99999");

        // 匀速线性插值器
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 当插值计算进行时，获取中间的每个值，
                // 这里这个值是中间过程中的曲线长度（下面根据这个值来得出中间点的坐标值）
                float value = (Float) animation.getAnimatedValue();
                System.out.println("value:"+value);
                imageView.setScaleX(1-value/1000);
                imageView.setScaleY(1-value/1000);
                // ★★★★★获取当前点坐标封装到mCurrentPosition
                // boolean getPosTan(float distance, float[] pos, float[] tan) ：
                // 传入一个距离distance(0<=distance<=getLength())，然后会计算当前距
                // 离的坐标点和切线，pos会自动填充上坐标，这个方法很重要。
                mPathMeasure.getPosTan(value, mCurrentPosition, null);//mCurrentPosition此时就是中间距离点的坐标值
                // 移动的商品图片（动画图片）的坐标设置为该中间点的坐标
                imageView.setTranslationX(mCurrentPosition[0]);
                imageView.setTranslationY(mCurrentPosition[1]);
            }
        });
        //开始执行动画
        valueAnimator.start();


      //  六、动画结束后的处理
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            //当动画结束后：
            @Override
            public void onAnimationEnd(Animator animation) {
                // 把移动的图片imageview从父布局里移除
                rl.removeView(imageView);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    private void initView() {



    }


}
